<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('我的通知')" />
</head>
<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="message-receive-form">
                    <div class="select-list">
                        <ul>
                            <li>
                                通知标题：<input type="text" name="title"/>
                            </li>
                            <li>
                                通知类型：<select name="messageType">
                                    <option value="">所有</option>
                                    <option value="1">系统通知</option>
                                    <option value="2">业务通知</option>
                                    <option value="3">公告</option>
                                    <option value="4">提醒</option>
                                </select>
                            </li>
                            <li>
                                阅读状态：<select name="readStatus">
                                    <option value="">所有</option>
                                    <option value="0">未读</option>
                                    <option value="1">已读</option>
                                </select>
                            </li>
                            <li>
                                处理状态：<select name="handleStatus">
                                    <option value="">所有</option>
                                    <option value="0">未处理</option>
                                    <option value="1">已处理</option>
                                    <option value="2">忽略</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="batchMarkAsRead()">
                    <i class="fa fa-check"></i> 批量标记已读
                </a>
                <a class="btn btn-info" onclick="refreshUnreadCount()">
                    <i class="fa fa-refresh"></i> 刷新未读数量
                </a>
                <span class="badge badge-danger" id="unreadCount" style="margin-left: 10px; padding: 5px 10px;">0</span>
            </div>
            
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var prefix = ctx + "system/message";
        var userId = "[[${curUserId}]]";
        var priorityDatas = [[${@dict.getType('sys_message_priority')}]];
        var typeDatas = [[${@dict.getType('	sys_message_type')}]];
        
        $(function() {
            // 加载未读数量
            refreshUnreadCount();
            
            var options = {
                url: prefix + "/user/list",
                createUrl: prefix + "/add",
                modalName: "消息",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'receiveId',
                    title: '序号',
                    visible: false
                },
                {
                    field: 'title',
                    title: '通知标题',
                    formatter: function(value, row, index) {
                        var readClass = row.readStatus == 0 ? 'text-danger font-weight-bold' : '';
                        return '<a href="javascript:void(0)" class="' + readClass + '" onclick="viewMessage(\'' + row.messageId + '\')">' + value + '</a>';
                    }
                },
                {
                    field: 'messageType',
                    title: '通知类型',
                    align: 'center',
                    formatter: function(value, row, index) {
                    	return $.table.selectDictLabel(typeDatas, value);
                        //var typeMap = {'1': '系统通知', '2': '业务通知', '3': '公告', '4': '提醒'};
                        //return typeMap[value] || value;
                    }
                },
                {
                    field: 'priority',
                    title: '优先级',
                    align: 'center',
                    formatter: function(value, row, index) {
                    	return $.table.selectDictLabel(priorityDatas, value);
                        //var priorityMap = {'1': '低', '2': '中', '3': '高', '4': '紧急'};
                        //var badgeClass = value == 1 ? 'badge-secondary' : value == 2 ? 'badge-info' : value == 3 ? 'badge-warning' : 'badge-danger';
                        //return '<span class="badge ' + badgeClass + '">' + (priorityMap[value] || value) + '</span>';
                    }
                },
                {
                    field: 'readStatus',
                    title: '阅读状态',
                    align: 'center',
                    formatter: function(value, row, index) {
                        return value == 1 ? '<span class="badge badge-success">已读</span>' : '<span class="badge badge-danger">未读</span>';
                    }
                },
                {
                    field: 'handleStatus',
                    title: '处理状态',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var statusMap = {'0': '未处理', '1': '已处理', '2': '忽略'};
                        var badgeClass = value == 0 ? 'badge-warning' : value == 1 ? 'badge-success' : 'badge-secondary';
                        return '<span class="badge ' + badgeClass + '">' + (statusMap[value] || value) + '</span>';
                    }
                },
                {
                    field: 'senderName',
                    title: '发送人',
                    align: 'center'
                },
                {
                    field: 'sendTime',
                    title: '发送时间',
                    align: 'center',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if (row.readStatus == 0) {
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="markAsRead(\'' + row.receiveId + '\')"><i class="fa fa-check"></i>标记已读</a> ');
                        }
                        if (row.handleStatus == 0) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="handleNotice(\'' + row.receiveId + '\', 1)"><i class="fa fa-check-circle"></i>已处理</a> ');
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="handleNotice(\'' + row.receiveId + '\', 2)"><i class="fa fa-times"></i>忽略</a> ');
                        }
                        actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="viewMessage(\'' + row.receiveId + '\')"><i class="fa fa-eye"></i>查看</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        
        // 刷新未读数量
        function refreshUnreadCount() {
            $.ajax({
                url: prefix + "/user/unread/count",
                type: "get",
                data: {userId: userId},
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $('#unreadCount').text(result.data);
                    }
                }
            });
        }
        
        // 标记为已读
        function markAsRead(messageId) {
            $.ajax({
                url: prefix + "/mark/read/" + messageId,
                type: "post",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        //$.modal.msgSuccess("标记成功");
                        $.table.refresh();
                        refreshUnreadCount();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
        
        // 批量标记已读
        function batchMarkAsRead() {
            var rows = $.table.selectFirstColumns();
            if (rows.length == 0) {
                $.modal.alertWarning("请选择要标记的通知");
                return;
            }
            
            $.ajax({
                url: prefix + "/mark/read/batch",
                type: "post",
                data: JSON.stringify(rows),
                contentType: "application/json",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        //$.modal.msgSuccess("批量标记成功");
                        $.table.refresh();
                        refreshUnreadCount();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
        
        // 处理通知
        function handleNotice(receiveId, handleStatus) {
            var handleRemark = '';
            if (handleStatus == 1) {
                handleRemark = prompt("请输入处理备注（可选）：");
                if (handleRemark === null) return;
            }
            
            $.ajax({
                url: prefix + "/handle/" + receiveId,
                type: "post",
                data: {
                    handleStatus: handleStatus,
                    handleRemark: handleRemark || ''
                },
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess("处理成功");
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
        
        // 查看通知详情
        function viewMessage(messageId) {
            $.modal.openTab("通知详情", prefix + "/view/" + messageId);
            // 自动标记为已读
            markAsRead(messageId);
        }
    </script>
</body>
</html>